﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using BCCommon;
using BCCommon.Enums;
using BCCommon.Extensions;
using BCService.Utils;
using BCTMSRESTful.Configs;
using BCService.Sim.Company.Company;
using BCDto.Sim.Company.Company;

namespace BCTMSRESTful.Controllers.Management.Department
{
    /// <summary>
    /// 公司
    /// </summary>
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class CompanyController : ManagementBaseController
    {
        private readonly ICompanyService companyService;

        public CompanyController(ICompanyService companyService)
        {
            this.companyService = companyService;
        }

        #region 添加公司
        /// <summary>
        /// 创建公司信息
        /// </summary>
        /// <response code="201">创建公司信息成功。</response>
        /// <response code="401">用户无权限访问。</response>
        [HttpPost]
        [Log("添加公司")]
        [ProducesResponseType(typeof(CompanyDto), 200)]
        public IActionResult Post([FromBody]CompanyRequestDto companyRequestDto)
        {
            var result = this.companyService.Add(this.Admin, companyRequestDto);
            return this.Created(string.Format("/api/v1/sim/Company/{0}", result.CompanyId), result);
        }
        #endregion

        #region 修改公司
        /// <summary>
        /// 修改公司信息
        /// </summary>
        /// <param name="companyId">公司Id</param>
        /// <param name="companyRequestDto"></param>
        /// <response code="201">修改公司信息成功。</response>
        /// <response code="401">用户无权限访问。</response>
        [HttpPut("{company_id}")]
        [Log("修改公司")]
        [ProducesResponseType(typeof(CompanyDto), 200)]
        public IActionResult Put([FromRoute(Name = "company_id")]int companyId, [FromBody]CompanyRequestDto companyRequestDto)
        {
            CompanyPutDto companyPutDto = companyRequestDto.As<CompanyPutDto>();
            companyPutDto.CompanyId = companyId;
            var result = this.companyService.Update(this.Admin, companyPutDto);
            return this.Ok(result);
        }
        #endregion

        #region 删除公司
        /// <summary>
        /// 删除公司
        /// </summary>
        /// <response code="204">删除公司。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <returns></returns>
        [HttpDelete("{company_id}")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Delete([FromRoute(Name = "company_id")]int companyId)
        {
            this.companyService.Delete(companyId);
            return this.NoContent();
        }
        #endregion

        /// <summary>
        /// 获取公司配置信息列表
        /// </summary>
        /// <response code="200">获取公司列表。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<CompanyDto>), 200)]
        public IActionResult List()
        {
            var list = this.companyService.GetList(this.Admin);
            return this.Ok(list);
        }

        /// <summary>
        /// 获取配置信息列表（分页）
        /// </summary>
        /// <param name="companyBusinessTypeCode">公司业务类型</param>
        /// <param name="companyName">公司名称</param>
        /// <param name="companyStatus">公司状态</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">开始时间</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="ascending">升序</param>
        /// <param name="index">当前页</param>
        /// <param name="size">每页条数</param>
        /// <response code="200">获取配置信息列表。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<CompanyDto>), 200)]
        public IActionResult Get([FromQuery(Name = "company_business_type_code")] string companyBusinessTypeCode,
            [FromQuery(Name = "company_name")] string companyName,
            [FromQuery(Name = "company_status")]CompanyStatus? companyStatus,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "sort_name")]string sortName,
            [FromQuery(Name = "ascending")]bool? ascending,
            [FromQuery(Name = "index")]int index = 1,
            [FromQuery(Name = "size")]int size = 10)
        {
            return this.Ok(this.companyService.Query(companyBusinessTypeCode, companyName, companyStatus, start, end, sortName, ascending, index, size));
        }

        /// <summary>
        /// 获取公司配置信息
        /// </summary>
        /// <param name="companyId">公司配置Id</param>
        /// <response code="200">获取公司配置信息。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <returns></returns>
        [HttpGet("{company_id}")]
        [ProducesResponseType(typeof(CompanyDto), 200)]
        public IActionResult Get([FromRoute(Name = "company_id")]int companyId)
        {
            return this.Ok(this.companyService.Get(companyId));
        }

        /// <summary>
        /// 获取可用开发票公司列表
        /// </summary>
        /// <param name="pageIndex">分页页码</param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<CompanyDto>), 200)]
        public IActionResult AvailableInvoiceList([FromQuery(Name = "page_index")] int pageIndex = 1,
        [FromQuery(Name = "page_size")]int pageSize = 10)
        {
            return this.Ok(this.companyService.Query(null, null, CompanyStatus.Running, null, null, null, true, pageIndex, pageSize));
        }
    }
}
